package com.carpedil.smvc.interceptor;

import com.carpedil.smvc.po.Permissions;
import com.carpedil.smvc.po.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/** spring 的interceptor拦截器,相对于filter而已,它只会拦截servlet */
public class LoginInterceptor implements HandlerInterceptor {
  /**
   * //进入Controller之间进行验证 //这个方法有返回值，如果为true，可以进入Controller //因此，一般我们需要验证的一些内容都放入到preHandle方法中
   *
   * @param request request
   * @param response response
   * @param handler handler
   * @return t/f
   * @throws Exception e
   */
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
      throws Exception {
    System.out.println("==========LoginInterceptor.preHandle==========");
    String uri = request.getRequestURI();
    System.out.println("uri = " + uri);

    boolean isRight = false;
    if (uri.indexOf("login") != -1) {
      return true;
    } else {
      HttpSession session = request.getSession();
      Object obj = session.getAttribute("loginUser");
      if (obj != null) {
        User user = (User) obj;
        List<Permissions> permissions = user.getPermissions();
        for (Permissions p : permissions) {
          if (p.getModuleUrl().equalsIgnoreCase(uri)) {
            isRight = true;
            break;
          }
        }
        if (isRight) {
          return true;
        } else {
          session.removeAttribute("loginUser");
          response.sendRedirect("/day06_SpringMvcDemo2/index.jsp");
        }
      } else {
        response.sendRedirect("/day06_SpringMvcDemo2/index.jsp");
      }
      return true;
    }
  }

  /**
   * //进入到Controller中可以执行的方法 //一般在这个方法中我们可以记录日志 //或者做一些内容的过滤
   *
   * @param request request
   * @param response response
   * @param handler handler
   * @param modelAndView modelAndView
   * @throws Exception Exception
   */
  @Override
  public void postHandle(
      HttpServletRequest request,
      HttpServletResponse response,
      Object handler,
      ModelAndView modelAndView)
      throws Exception {
    System.out.println("==========LoginInterceptor.postHandle==========");
    System.out.println("handler = " + handler);
  }

  /**
   * //执行完jsp之后，执行afterCompletion //一般这里面就记录日志或者处理异常
   *
   * @param request request
   * @param response response
   * @param handler handler
   * @param ex ex
   * @throws Exception Exception
   */
  @Override
  public void afterCompletion(
      HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
      throws Exception {
    System.out.println("==========LoginInterceptor.afterCompletion==========");
  }
}
